--
-- Created by IntelliJ IDEA.
-- User: xiaoyuan
-- Date: 4/21/15
-- Time: 6:02 PM
-- To change this template use File | Settings | File Templates.
--



init("0", 2)
luaExitIfCall(true)



SCREEN_HEIGHT, SCREEN_WIDTH = getScreenSize();
COCAPP_IDENTIFIER = "com.supercell.magic"

function ts_frontapp()
    front_app = frontAppBid();
    if front_app ~= COCAPP_IDENTIFIER then
        return false;
    else
        return true;
    end
end




function touchDU(x, y)
    touchDown(1, x, y)
    mSleep(50)
    touchUp(1, x, y)
    mSleep(50)
end


function smallShrink()
    touchDU(10, 10)

    number = 5
    touchDown(1, SCREEN_WIDTH / number, SCREEN_HEIGHT / 2)
    touchDown(2, SCREEN_WIDTH * (number - 1) / number, SCREEN_HEIGHT / 2)
    mSleep(50)

    for i = 1, 400, 50 do
        touchMove(1, SCREEN_WIDTH / number + i, SCREEN_HEIGHT / 2)
        touchMove(2, SCREEN_WIDTH * (number - 1) / number - i, SCREEN_HEIGHT / 2)
        mSleep(50)
    end

    touchUp(1, SCREEN_WIDTH / number + 400, SCREEN_HEIGHT / 2)
    touchUp(2, SCREEN_WIDTH * (number - 1) / number - 400, SCREEN_HEIGHT / 2)

    mSleep(200)
end

function moveUp()
    touchDU(10, 10)
    touchDown(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4)
    mSleep(50)
    for i = 1, SCREEN_HEIGHT / 2, 50 do
        touchMove(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 + i)
        mSleep(50)
    end
    touchUp(1, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4)

    mSleep(200)
end

function shrinkAndTop()
    smallShrink()
    smallShrink()
    moveUp()
end

function splitPart()

    keepScreen(true)

    DX, DY = 20, 20

    --    { 1922, 635, 0xa1b64a},
    --    { 2053, 769, 0x546925},

    for i = 1922, 2053, DX do
        for j = 635, 769, DY do
            snapshot(i .. "_" .. j .. ".png", i, j, i + DX, j + DY);
            mSleep(50)
        end
    end

    keepScreen(false)
end




function trainSoliders()
    touchDU(10, 10)

    --    点击兵营
    x, y = findImageInRegionFuzzy("camp.png", 60, 0, 0, 2207, 1241, 0xffffff)

    if x ~= -1 then
        touchDown(1, x, y)
        mSleep(50)
        touchUp(1, x, y)
    else
        dialog("no found", 0)
    end

    mSleep(1000)

    --    点击训练剑

    x, y = findImageInRegionFuzzy("train_sword.png", 60, 0, 0, 2207, 1241, 0xffffff)

    if x ~= -1 then
        touchDown(1, x, y)
        mSleep(50)
        touchUp(1, x, y)
    else
        dialog("no found", 0)
    end

    mSleep(1000)
end


while true
do
    --1. 判断当前状态
    -- if 在coc
    -- then
    --
    -- else
    -- lauch coc



    shrinkAndTop()
    shrinkAndTop()

    trainSoliders()
    --    splitPart()

    if ts_frontapp() == true then
        dialog("is coc" .. SCREEN_WIDTH .. "x" .. SCREEN_HEIGHT, 3)
    else
        dialog("no coc" .. SCREEN_WIDTH .. "x" .. SCREEN_HEIGHT, 3)
    end


    --2. 根据状态进行操作

    --3. 每次休眠一段随机一段 TODO

    break
end
